<template>
  <div class="iframeDiv">
    <div v-if="isError" class="common-error-wrap">
      <common-error message="服务异常!" :home="true" :back="true"></common-error>
    </div>
    <iframe v-if="!isError && url" :src="url" frameborder="0"/>
  </div>
</template>

<script>
import { getTokenDecrypt } from '@/utils/auth'
import { checkActiveService } from '@/api/system'

export default {
  name: 'Apply',
  components: {},
  data() {
    return {
      url: '',
      token: getTokenDecrypt(),
      isError: false
    }
  },
  methods: {
    checkActiveService() {
      checkActiveService().then(res => {
        if (res && res.code === 200) {
          if (res.data) {
            this.handlePath()
            this.isError = false
          } else {
            this.isError = true
          }
        } else {
          this.isError = true
        }
      })
    },
    handlePath() {
      let url = (this.$route.meta && this.$route.meta.params) ? decodeURIComponent(this.$route.meta.params) : ''

      let params = this.$route.query
      console.log('路由参数: ', params)

      if (url && url.length > 5) {
        let path = url.substring(5, url.length)
        if (path) {
          console.log('工单系统路由: ', path)
          path = `${path}?nomenu=true&token=${this.token}`
          if (params) {
            let paramsStr = Reflect.ownKeys(params).reduce((r, c) => {
              r += `&${c}=${params[c]}`
              return r
            }, '')
            if (paramsStr) {
              path = `${path}${paramsStr}`
            }
          }

          console.log('工单: ', path)

          this.url = path
        }
      } else {
        console.log('当前路由信息: ', this.$route)
        let userName = this.$store.getters.name
        console.log('当前登录用户: ', userName)

        if (this.$route?.meta?.remark) {
          try {
            let { url } = JSON.parse(this.$route?.meta?.remark)
            if (url) this.url = `${url}?userCode=${userName}`
          } catch (e) {
            console.error('[ remark ] 转换异常: ', e)
          }
        } else {
          //
        }

      }
    }
  },
  created() {
    this.checkActiveService()

  }
}
</script>

<style lang="scss">
.iframeDiv {
  position: absolute;
  width: 100%;
  height: 100%;

  .common-error-wrap {
    width: 100%;
    height: 100%;
  }

  iframe {
    width: 100%;
    height: 100%;
  }
}
</style>
